/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EnumSet;

import org.lwjgl.util.Rectangle;

import fi.honkalampisaatio.lmodeller.LanguageResource;
import fi.honkalampisaatio.lmodeller.LModeller;
import fi.honkalampisaatio.lmodeller.Logger;
import fi.honkalampisaatio.lmodeller.selection.GameModeSelectionEvent;
import fi.honkalampisaatio.lmodeller.ui.GameButton.TEXTLOCATION;
import fi.honkalampisaatio.lmodeller.xml.User;
import fi.honkalampisaatio.lwjgl.GLTexture;
import glapp.GLApp;

public class BuildMenu extends GameView implements ActionListener {

	public BuildMenu() {
		super(EnumSet.of(BUTTONFLAGS.BACK, BUTTONFLAGS.HOME, BUTTONFLAGS.UPLABEL, BUTTONFLAGS.VOICE));
	}
	
    public void init()
    {
    	int h = GLApp.getHeight()/10;
    	int w = GLApp.getWidth()/10;
    	int minFontSize, tmpFontSize;
    	GameButton random_step_Game = new GameButton(new Rectangle((int)(w*6),3*h,w*3,h*4), GameTextures.getInstance().getTexture("RANDOM_STEP_GAME_BUTTON")
    			, LModeller.actionColor, null, TEXTLOCATION.UP);
    	GameTextLabel random_step_GameLabel = new GameTextLabel(new Rectangle((int)(w*6),h*7,w*3,h), 
    			(int)(h*0.8), LModeller.actionColor);
    	tmpFontSize = random_step_GameLabel.setText(LanguageResource.getInstance().get("RANDOM_MODEL"));
    	minFontSize = tmpFontSize;
    	GameButton modelGame = new GameButton(new Rectangle(w,3*h,w*3,h*4), GameTextures.getInstance().getTexture("MODELGAME_BUTTON")
    			, LModeller.actionColor, null, TEXTLOCATION.UP);
    	GameTextLabel modelGameLabel = new GameTextLabel(new Rectangle(w,h*7,w*3,h), 
    			(int)(h*0.8), LModeller.actionColor);
    	tmpFontSize = modelGameLabel.setText(LanguageResource.getInstance().get("FIGURATIVE_MODELS"));
    	if(tmpFontSize<minFontSize) {
        	minFontSize = tmpFontSize;
    	}
    	random_step_Game.setOnOff(false);
    	modelGame.setOnOff(false);
    	
    	tmpFontSize = setUpLabelText(LanguageResource.getInstance().get("CHOOSE_GAME"));
    	if(tmpFontSize<minFontSize) {
        	minFontSize = tmpFontSize;
    	}
    	
    	random_step_GameLabel.setMaxFontSize(minFontSize);
    	modelGameLabel.setMaxFontSize(minFontSize);

    	super.addComponent(random_step_GameLabel);
    	super.addComponent(random_step_Game);
    	random_step_Game.addActionListener(this);
    	random_step_Game.setActionCommand("RANDOM_MODEL");

    	super.addComponent(modelGameLabel);
    	super.addComponent(modelGame);
    	modelGame.addActionListener(this);
    	modelGame.setActionCommand("FIGURATIVE_MODELS");

    	random_step_Game.setToolTipSentence(LanguageResource.getInstance().get("RANDOM_MODEL"));
    	modelGame.setToolTipSentence(LanguageResource.getInstance().get("FIGURATIVE_MODELS"));
    	setUpToolTipSentence(LanguageResource.getInstance().get("CHOOSE_GAME"));
    	setInstructionSentence(LanguageResource.getInstance().get("CHOOSE_GAME"));
    }

	public void setUser(User user, GLTexture userTexture) {
		if(user!=null)
		{
			setUpLabelText(user.getName());
		}
		else
		{
			setUpLabelText(LanguageResource.getInstance().get("CHOOSE_GAME"));
		}
    	setUpToolTipSentence(getUpLabelText());
		setUpLabelTexture(userTexture);
	}

	@Override
	protected void refreshPage(int page) {
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("RANDOM_MODEL"))
		{
			Logger.getInstance().log("Random Step Game selected");
			GameModeSelectionEvent se = new GameModeSelectionEvent(this, GameModeSelectionEvent.MODES.RANDOM_STEP_GAME);
			this.fireSelectionEvent(se);
		}
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("FIGURATIVE_MODELS"))
		{
			Logger.getInstance().log("Model Game selected");
			GameModeSelectionEvent se = new GameModeSelectionEvent(this, GameModeSelectionEvent.MODES.MODEL_GAME);
			this.fireSelectionEvent(se);
		}
	}
}
